package cn.bytecollege.user.controller;

import cn.bytecollege.admin.service.PaymentService;
import cn.bytecollege.admin.service.ProductService;
import cn.bytecollege.common.entity.*;
import cn.bytecollege.user.service.AddressService;
import cn.bytecollege.user.service.OrderDetailService;
import cn.bytecollege.user.service.OrdersService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.crypto.Data;
import java.io.IOException;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Random;

@WebServlet(name = "OrdersSaveController",value = "/user/orders")
public class OrdersSaveController extends HttpServlet {
    OrderDetailService orderDetailService=null;
    AddressService addressService=null;
    PaymentService paymentService=null;
    OrdersService ordersService=null;
    public OrdersSaveController(){
        addressService=new AddressService();
        paymentService=new PaymentService();
        orderDetailService=new OrderDetailService();
        ordersService=new OrdersService();
    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        ProductService productService=new ProductService();
        String addressId=request.getParameter("addressId");
        String paychose=request.getParameter("paychose");
        HttpSession session=request.getSession();
        User user=(User) session.getAttribute("user");
        Integer userId=user.getUserId();
        Orders orders=new Orders();
        List<Address> addresseslist=addressService.findlistById(userId);
        List<OrderList> orderLists=orderDetailService.findByUserId(userId);
        Integer sumProduct=0;
        BigDecimal sumOrders= BigDecimal.valueOf(0);
        for(int k=0;k<orderLists.size();k++){
            sumProduct+=orderLists.get(k).getProductNumber();
            BigDecimal m=BigDecimal.valueOf(orderLists.get(k).getProductNumber());
            BigDecimal n=orderLists.get(k).getProductPrice();
            sumOrders = sumOrders.add(m.multiply(n));
        }
        Address address=addresseslist.get(Integer.valueOf(addressId));
     orders.setAddressId(address.getAddressId());
     orders.setPaymentName(paychose);
     orders.setPaymentState(1);
     orders.setProductSumPrices(sumOrders);
     Random r1=new Random(100);
     Date date=new Date();
     long r2=date.getTime();
     Long ordernumber= r2+r1.nextInt();
     orders.setOrderNumber(ordernumber);
     orders.setUserId(userId);
     ordersService.save(orders);
     Orders orders1=ordersService.findById(userId);
     for(int j=0;j<orderLists.size();j++){
         OrderDetail orderDetail=orderDetailService.findById(orderLists.get(j).getOrdersDetailId());
         orderDetail.setOrdersId(orders1.getOrdersId());
         orderDetailService.update(orderDetail);
     }
        List<Product> lists=productService.findAll();
        request.setAttribute("lists",lists);
     response.sendRedirect("/ok.jsp");


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        HttpSession session= request.getSession();
        User user=(User) session.getAttribute("user");
        Integer userId=user.getUserId();
        List<Address> addresseslist=addressService.findlistById(userId);
        request.setAttribute("addresseslist",addresseslist);
        List<OrderList> orderLists=orderDetailService.orderLists(userId);
        request.setAttribute("orderLists",orderLists);
        List<Payment> paymentList=paymentService.find();
        request.setAttribute("paymentList", paymentList);
        Integer sumProduct=0;
        BigDecimal sumOrders= BigDecimal.valueOf(0);
        for(int k=0;k<orderLists.size();k++){
            sumProduct+=orderLists.get(k).getProductNumber();
            BigDecimal m=BigDecimal.valueOf(orderLists.get(k).getProductNumber());
            BigDecimal n=orderLists.get(k).getProductPrice();
            sumOrders = sumOrders.add(m.multiply(n));
        }
        request.setAttribute("sumProduct",sumProduct);
        request.setAttribute("sumOrders",sumOrders);
        request.getRequestDispatcher("/order.jsp").forward(request,response);


    }
}
